module xenActuator;

import tango.time.Clock;
import tango.math.Math;

struct ACT_RESULT
{
	bool finished=true;
	float value=0f;
}



class Actuator
{
	float X,Y,startT,endT,i_duration;
	Time startTime,endTime;
	long duration;
	Clock clock;
	
	this(float iStart, float iEnd, long ms)
	{
		restart(iStart,iEnd,ms);
	}
	void restart(float iStart, float iEnd, long ms)
	{
		
		X=iStart;
		Y=iEnd;
		clock=Clock();
		duration=ms;
		startTime=clock.now;
		i_duration=1f/duration;
		
		endTime=startTime+TimeSpan.fromMillis(duration);
	}
	ACT_RESULT val()
	{
		ACT_RESULT res;
		
		auto t=endTime-clock.now;
		if (t>TimeSpan.zero)
		{
			t=clock.now-startTime;
			res.value=func(t.millis);
			res.finished=false;
			
		}else
		{
			res.value=Y;
		}
		return res;
	}
	
	float func(long t)
	{
		float dt=cast(float)(t)/duration;
		return X+dt*(Y-X);
	}
}

class SineActuator :Actuator
{
	this(float iStart, float iEnd, long ms)
	{
		super( iStart, iEnd, ms);
	}
	float func(long t)
	{
		float dt=(cast(float)(t))/duration;
		float phi=3.171592*0.5f*(2*dt-1);
		return (1+sin(phi))*0.5*(Y-X)+X;
	}
}
